package io.github.mobodev.accessbilityservicetest;

import android.content.Context;
import android.graphics.Typeface;
import android.hardware.display.DisplayManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.PowerManager;
import android.os.Vibrator;
import android.util.Log;
import android.view.Display;

/**
 * Created by bshao on 5/5/16.
 */
public class Utils {
    public static String TAG;

    static {
        TAG = "callvib";
    }

    public static boolean disableVibrationWithHeadset(Context context) {
        boolean headsetDisabled;
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        if (PreferencesController.getEnableHeadset(context) || !audioManager.isWiredHeadsetOn()) {
            headsetDisabled = false;
        } else {
            headsetDisabled = true;
        }
        if (headsetDisabled) {
            return true;
        }
        return false;
    }

    public static boolean disableVibrationWithApproximity(Context context) {
        boolean approximityDisabled;
        boolean isScreenOn = true;
        if (Build.VERSION.SDK_INT >= 20) {
            for (Display display : ((DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE)).getDisplays()) {
                if (display.getState() == 1) {
                    Log.i(TAG, "Screen is off");
                    isScreenOn = false;
                }
            }
        } else {
            isScreenOn = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).isScreenOn();
        }
        if (PreferencesController.getEnableProximitySensor(context) || isScreenOn) {
            approximityDisabled = false;
        } else {
            approximityDisabled = true;
        }
        if (approximityDisabled) {
            return true;
        }
        return false;
    }

    public static void vibrateDevice(Context context, boolean isStartCall) {
        long vibrateDuration;
        int vibrateCount;
        if (isStartCall) {
            vibrateDuration = (long) ((PreferencesController.getStartCallVibrationDuration(context) + 1) * 100);
            vibrateCount = PreferencesController.getStartCallVibrationCount(context);
        } else {
            vibrateDuration = (long) ((PreferencesController.getEndCallVibrationDuration(context) + 1) * 100);
            vibrateCount = PreferencesController.getEndCallVibrationCount(context);
        }
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrateCount != 0) {
            long[] pattern = new long[(((vibrateCount + 1) * 2) + 1)];
            pattern[0] = 0;
            for (int i = 1; i < pattern.length - 1; i += 2) {
                pattern[i] = vibrateDuration;
                pattern[i + 1] = 100;
            }
            if (v.hasVibrator()) {
                v.vibrate(pattern, -1);
            }
        } else if (v.hasVibrator()) {
            v.vibrate(vibrateDuration);
        }
    }

    public static Typeface getFont(Context context) {
        return Typeface.createFromAsset(context.getAssets(), "Roboto-Black.ttf");
    }
}
